package com.punch.system.controller;


import com.punch.common.dto.system.dict.DictDTO;
import com.punch.common.dto.system.dict.DictKeyDTO;
import com.punch.common.dto.system.dict.RedisDictDTO;
import com.punch.common.exception.BusinessException;
import com.punch.common.model.Result;
import com.punch.common.page.PageInfo;
import com.punch.common.vo.system.dict.PageDictVO;
import com.punch.system.exception.SysExceptionEnum;
import com.punch.system.model.SysDictKey;
import com.punch.system.query.DictQueryInfo;
import com.punch.system.service.DictKeyService;
import com.punch.system.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * DictController- 字典表 前端控制器
 *
 * @author wujj
 * @version 1.0.0
 * @since 2020/6/3 19:47
 */
@RestController
@RequestMapping("/sys/dict")
@Api(value = "DictController", tags = "字典管理Controller")
public class DictController {


    @Autowired
    private DictKeyService dictKeyService;

    @Autowired
    private DictService dictService;


    /**
     * 系统下字典类型列表
     *
     * @param queryInfo 查询条件
     * @return PageInfo<PageDictVO>
     */
    @GetMapping(value = "/key", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "系统下字典类型列表", notes = "系统下字典类型列表", produces = "application/json")
    public Result<PageInfo<PageDictVO>> listDictkey(@ModelAttribute DictQueryInfo queryInfo) {
        return Result.succeed(dictKeyService.queryPage(queryInfo));
    }


    /**
     * 保存数据字典类型
     *
     * @param dictKeyDTO 资源数据字典类型表单
     * @return dictKeyId
     * @author wujj
     * @since 2020/3/10 4:28 下午
     */

    @PostMapping(value = "/key", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "保存数据字典类型", notes = "保存数据字典类型", produces = "application/json")
    public Result<Long> saveDictKey(@Valid @RequestBody DictKeyDTO dictKeyDTO) {
        return Result.succeed(dictKeyService.saveDictKey(dictKeyDTO));
    }


    /**
     * 修改数据字典类型
     *
     * @param dictKeyDTO 资源数据字典类型表单
     * @return dictKeyId
     * @author wujj
     * @since 2020/6/4 14:35
     */
    @PutMapping(value = "/key", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "修改数据字典类型", notes = "修改数据字典类型", produces = "application/json")
    public Result<Long> updateDictKey(@Valid @RequestBody DictKeyDTO dictKeyDTO) {
        return Result.succeed(dictKeyService.updateDictKey(dictKeyDTO));
    }

    /**
     * 删除数据字典类型
     *
     * @param dictKeyId 资源数据字典类型id
     * @return dictKeyId
     * @author wujj
     * @since 2020/6/4 14:38
     */
    @DeleteMapping(value = "/key/{dictKeyId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除数据字典类型", notes = "删除数据字典类型", produces = "application/json")
    public Result<Long> deleteDictKey(@PathVariable Long dictKeyId) {
        return Result.succeed(dictKeyService.deleteDictKey(dictKeyId));
    }

    /**
     * 启用/停用数据字典
     *
     * @param dictKeyId 资源数据字典类型id
     * @return dictKeyId
     * @author wujj
     * @since 2020/6/4 14:38
     */
    @PutMapping(value = "/key/{dictKeyId}/status", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "启用/停用数据字典", notes = "启用/停用数据字典", produces = "application/json")
    public Result<Long> updateDictKeyStatus(@PathVariable Long dictKeyId) {
        return Result.succeed(dictKeyService.updateDictKeyStatus(dictKeyId));
    }

    /*********************************************value******************************************************/
    /**
     * 字典key下字典内容与值
     *
     * @param dictKeyId 字典keyId
     * @return List<DictDTO>
     * @author wujj
     * @author wujj
     * @since 2020/6/4 14:27
     */
    @GetMapping(value = "/key/{dictKeyId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "字典key下字典内容与值", notes = "字典key下字典内容与值", produces = "application/json")
    public Result<List<DictDTO>> listDictByKey(@PathVariable Long dictKeyId) {
        return Result.succeed(dictService.listDictByKey(dictKeyId));
    }

    /**
     * 保存数据字典值和内容
     *
     * @param dictKeyId 字典keyId
     * @param dictList  资源数据字典列表
     * @return dictKeyId
     * @author wujj
     * @since 2020/3/10 4:28 下午
     */
    @PostMapping(value = "/key/{dictKeyId}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "保存数据字典值和内容", notes = "保存数据字典值和内容", produces = "application/json")
    public Result<Long> saveDict(@PathVariable Long dictKeyId, @RequestBody List<DictDTO> dictList) {
        SysDictKey sysDictKeyEX = dictKeyService.getById(dictKeyId);
        if (sysDictKeyEX == null) {
            throw new BusinessException(SysExceptionEnum.DICT_NOT_EXIST);
        }
        return Result.succeed(dictService.saveDict(dictKeyId, sysDictKeyEX.getDictKey(), dictList));
    }

    /*********************************************查找******************************************************/
    /**
     * 根据keys查找多个字典列表
     *
     * @param keys 类型列表
     * @return HashMap<String, List < SysDict>>
     * @author wujj
     * @since 2020/6/4 16:14
     */
    @GetMapping(value = "/{keys}", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "根据keys查找多个字典列表", notes = "keys是多个type以','分隔拼接的字符串", produces = "application/json")
    public Result<HashMap<String, List<RedisDictDTO>>> listMultipleDict(@PathVariable String keys) {
        return Result.succeed(dictService.listMultipleDict(keys));
    }

}
